package com.cheyunkeji.er.activity.auction;

import android.widget.TextView;

import com.cheyun.netsalev3.MyApplication;
import com.cheyun.netsalev3.R;
import com.cheyunkeji.er.MyFragmentManager;
import com.cheyunkeji.er.adapter.MyFragmentPagerAdapter;
import com.cheyunkeji.er.base.BaseActivity;
import com.cheyunkeji.er.bean.MsgCountWrapper;
import com.cheyunkeji.er.fragment.auction.AuctionRecordLost;
import com.cheyunkeji.er.fragment.auction.AuctionRecordMine;
import com.cheyunkeji.er.fragment.auction.AuctionRecordOthersGot;
import com.cheyunkeji.er.utils.TabLayoutAdjustBoundUtils;
import com.cheyunkeji.er.view.TopBar;
import com.google.android.material.tabs.TabLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.ButterKnife;

public class AuctionRecordActivity extends BaseActivity {

    @BindView(R.id.v_topbar)
    TopBar vTopbar;
    @BindView(R.id.tl_auction_record)
    TabLayout tlAuctionRecord;
    @BindView(R.id.vp_auction_record)
    ViewPager vpAuctionRecord;
    @BindView(R.id.tv_item_count)
    TextView tvItemCount;

    private ArrayList<String> titleList;
    private ArrayList<Fragment> fragList;
    private MyFragmentPagerAdapter adapter;

    @Override
    protected void init() {

        EventBus.getDefault().register(this);
        setContentView(R.layout.er_activity_auction_record);
        ButterKnife.bind(this);

        titleList = new ArrayList<>();
        fragList = new ArrayList<>();
        titleList.add(MyApplication.getInstance().getString(R.string.debiao));
        titleList.add(MyApplication.getInstance().getString(R.string.liubiao));
        titleList.add(MyApplication.getInstance().getString(R.string.others));

        fragList.add(MyFragmentManager.getInstance().getFrag(AuctionRecordMine.class));
        fragList.add(MyFragmentManager.getInstance().getFrag(AuctionRecordLost.class));
        fragList.add(MyFragmentManager.getInstance().getFrag(AuctionRecordOthersGot.class));
    }

    @Override
    protected void initView() {
        vTopbar.setTitle(getString(R.string.auction_record));
        vTopbar.setLeftBack();
        adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), titleList, fragList);
        vpAuctionRecord.setAdapter(adapter);
        tlAuctionRecord.setupWithViewPager(vpAuctionRecord);
        tlAuctionRecord.setTabsFromPagerAdapter(adapter);
//       调整Tab头部间距 使用的是反射，在proguard文件中一定要设置以便防止混淆导致找不到方法、属性错误
        TabLayoutAdjustBoundUtils.setIndicator(this, tlAuctionRecord, 30, 30);
        TabLayoutAdjustBoundUtils.setTabDivider(tlAuctionRecord);
    }

    @Override
    protected void initData() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMsgCountChanged(MsgCountWrapper wrapper){

        if(vpAuctionRecord.getCurrentItem() == wrapper.pageIndex ){
            setItemCount(wrapper.msgCount);
        }

    }

    public void setItemCount(int itemCount ){
        tvItemCount.setText(String.format( "共%1$s条数据" , String.valueOf(itemCount)));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
